#!/usr/bin/env python3
import xml.etree.ElementTree as ET
from designpattern import Singleton

class User(object):
    name = None
    password = None

    def __init__(self, name, pwd):
        self.name = name
        self.password = pwd

    def getName(self):
        return self.name

    def getPwd(self):
        return self.password

    def __str__(self):
        return "name:%s----pwd:%s" % (self.name, self.password)

class UserService(object, metaclass=Singleton):

    configPath = '../config/userconfig.xml'
    root = ET.parse(configPath).getroot()

    def __init__(self):
        super(UserService, self).__init__()
        
    def getUsers(self):
        userList = []
        for user in self.root:
            userList.append(User(user[1][0].text, user[1][1].text))
        return userList

    def getUser(self, name):
        userList = self.getUsers()
        for item in userList:
            if item.getName() == name:
                return item

    def checkUser(self, name, pwd):
        user = self.getUser(name)
        if not user:
            return False
        return pwd == user.getPwd()


if __name__ == '__main__':
    print(UserService().checkUser('sayi', 'hello'))
    userList = UserService().getUsers()
    for item in userList:
        print(item)
    print(UserService().getUser('ada'))
